export default {
	data() {
		return {
			order_id: 0,
			label: [{
				text: '全部',
				name: 'first'
			}, {
				text: '待支付',
				name: 'second'
			}, {
				text: '待接收',
				name: 'third'
			}, {
				text: '待返还',
				name: 'fourth'
			}, {
				text: '待验收',
				name: 'fifth'
			}, {
				text: '待退款',
				name: 'sixth'
			}, {
				text: '已退款',
				name: 'seventh'
			}, {
				text: '已完成',
				name: 'eighth'
			}],

			label_one:[{
                id: 1,
                name: '按下单时间排序'
			},{
                id: 2,
                name: '按短购天数排序'
            },{
                id: 3,
                name: '按应付总额排序'
            }],
            indx:0,
			orderList: [],
			activeName: 'first',
            goods_sn: '',
			date: '',
			type: 1,
			src: '',
			start_time: '',
			end_time: '',
			count: 0,
			pageNum: 1,      //分页数
			limit: 10,		//条数
            time_line:'',
            buy_line:'',
            pay_line:'',
            time_sort:false,
            buy_sort:false,
            pay_sort:false
		}
	},
	//进入页面加载
	mounted: function() {
		var that = this;
		//在缓存中获取值
		that.user_name = sessionStorage.getItem("user_name");
		that.user_id = sessionStorage.getItem("user_id");
		that.token = sessionStorage.getItem("access-token");
		if((!that.token) || (!that.user_id) || (!that.user_name)) {
			that.$router.push('/login');
		}
		
		//获取商品
		that.getOrderList(that.pageNum, that.limit);
	},
	methods: {
        /**
		 * 选择类型
         * @param tab
         */
		handleClick(tab) {
			let that = this;
			if(tab.name === 'first') {
				that.type = 1;
			}
			if(tab.name === 'second') {
				that.type = 2;
			}
			if(tab.name === 'third') {
				that.type = 3;
			}
            if(tab.name === 'fourth') {
                that.type = 4;
            }
			if(tab.name === 'fifth') {
                that.type = 5;
            }
            if(tab.name === 'sixth') {
                that.type = 6;
            }
            if(tab.name === 'seventh') {
                that.type = 7;
            }
            if(tab.name === 'eighth') {
                that.type = 8;
            }
			that.getOrderList(that.pageNum, that.limit);
		},

        /**
		 * 选择筛选条件(时间排序等)
         */
        checkSearch:function (e,index) {
			let that = this;
			that.indx = index;
			if(e === 1){
				that.time_line = 1;
                that.buy_line = '';
                that.pay_line = '';
                that.getOrderList(that.pageNum, that.limit);
			}else if(e === 2){
                that.time_line = '';
                that.buy_line = 1;
                that.pay_line = '';
                that.getOrderList(that.pageNum, that.limit);
			}else {
                that.time_line = '';
                that.buy_line = '';
                that.pay_line = 1;
                that.getOrderList(that.pageNum, that.limit);
			}
        },

        /**
         * 下一页
         * @param pageNum
         */
        handleCurrentChange: function(pageNum) {
            var that = this;
            that.pageNum = pageNum;
            // that.orderList = that.list.slice((pageNum - 1) * that.limit, pageNum * that.limit);
            that.getOrderList(that.pageNum, that.limit);
        },


        /**
		 * 订单列表
         * @param pageNum
         * @param limit
         */
		getOrderList: function(pageNum, limit) {
			var that = this;
			//初始化数据
			that.list = [];
			// that.count = 0;
			//请求的参数
			var formData = {};
			formData.type = that.type;
			formData.token = that.token;
            formData.pageNum = pageNum;
            formData.pageSize = limit;

			//查询内容
			if(that.goods_sn) {
				formData.sn = that.goods_sn;
			}

			//时间条件
			if(that.date) {
				that.start_time = that.formatDateTime(that.date[0]);
				that.end_time = that.formatDateTime(that.date[1]);
				formData.start_time = that.formatDateTime(that.date[0]);
				formData.end_time = that.formatDateTime(that.date[1]);
			}

			if(that.time_line){
                formData.create_time = that.time_line;
                that.time_sort = !that.time_sort;
                formData.time_sort = that.time_sort;
			}
            if(that.buy_line){
                formData.goods_spec_id = that.buy_line;
                that.buy_sort = !that.buy_sort;
                formData.buy_sort = that.buy_sort;
            }
            if(that.pay_line){
                formData.total_price = that.pay_line;
                that.pay_sort = !that.pay_sort;
                formData.pay_sort = that.pay_sort;
            }
			//请求接口
			that.$http.post(that.adminApi.api_url + "/api/order/index", formData, {
				emulateJSON: true
			}).then(
				function(res) {
					// 处理成功的结果
					//获取状态
					if(res.body.list !== 0) {
                        for (var i in res.body.list) {
                            res.body.list[i].status_text = that.getOrderStatus(res.body.list[i]);
                            that.list.push(res.body.list[i]);
                        }
                    }else {
                        that.list = [];
					}
                    that.orderList = that.list;
                    that.pageNum = pageNum;
					// that.orderList = that.list.slice((pageNum - 1) * that.limit, pageNum * limit);
					if(res.body.count) {
						that.count = res.body.count;
					} else {
						that.count = 0;
					}

				},
				function() {
					// 处理失败的结果
					that.$message({
						type: 'error',
						message: `操作提示: ${ '处理异常' }`
					});
				});
		},

        /**
		 * 获取订单状态
         * @param res
         * @returns {string}
         */
		getOrderStatus: function(res) {
			var status_text = "";
			switch(res.status) {
				case 1:
                    if ((res.is_pay === 3) && (res.is_cancel === 4)){
                        status_text = '正常-待付款'
                    }
                    if ((res.is_pay !== 3) && (res.is_deliver === 3) ){
                        status_text = '正常-支付成功'
                    }
                    if ((res.is_pay !== 3) && (res.is_deliver === 1) && (res.is_receipt === 3)) {
                        status_text = '正常-商品配送中'
                    }
                    if ((res.is_pay !== 3) && (res.is_receipt === 1) && (res.is_deliver === 1)&& (res.is_pickup === 3)) {
                        status_text = '正常-商品已签收'
                    }

                    if ((res.is_pay !== 3) && (res.is_pickup === 1) && (res.is_receipt === 1) && (res.is_deliver === 1)) {
                        status_text = '正常-商品取回中'
                    }

                    if ((res.is_pay !== 3) && (res.is_sign_back === 1) && (res.is_receipt === 1) && (res.is_deliver === 1)) {
                        status_text = '正常-商品已签还'
                    }
                    if ((res.is_pay !== 3) && (res.is_sign_back === 1) && (res.is_receipt === 1) && (res.is_deliver === 1)  && (res.check_accept_status === 1)) {
                        status_text = '正常-商品验收正常'
                    }
                    if ((res.is_pay !== 3) && (res.is_sign_back === 1) && (res.is_receipt === 1) && (res.is_deliver === 1)  && (res.check_accept_status === 2)) {
                        status_text = '正常-商品验收异常'
                    }
                    if ((res.is_pay !== 3) && (res.is_sign_back === 1) && (res.is_receipt === 1) && (res.is_deliver === 1)  && (res.is_check_accept === 1) && (res.is_refund ===1)) {
                        status_text = '正常-退款已汇出'
                    }
                    if ((res.is_over === 1)) {
                        status_text = '正常-订单已完成'
                    }
                    break;
				case 2:
                    if ((res.is_pay === 3) && (res.is_cancel === 2)) {
                        status_text = '正常-支付超时'
                    }
                    if ((res.is_pay !== 3) && (res.is_cancel === 3) && (res.is_deliver === 3) && (res.is_refund === 2)) {
                        status_text = '取消-支付成功'
                    }
                    if ((res.is_pay !== 3) && (res.is_refund === 3) && (res.is_cancel === 3)) {
                    	status_text = '取消-支付成功'
                	}
                    if ((res.is_pay === 3) && (res.is_cancel === 3)) {
                        status_text = '取消-后台取消'
                    }

                    if ((res.is_pay !== 3) && (res.is_deliver === 1)) {
                        status_text = '取消-商品配送中'
                    }
                    if ((res.is_pay !== 3) && (res.is_refund === 1) && (res.is_cancel === 3)) {
                        status_text = '取消-退款已汇出'
                    }
                    if ((res.is_over === 1)) {
                        status_text = '取消-订单已完成'
                    }
                    break;
				default:
					status_text = '订单已完成';
                    break;
			}
			return status_text;
		},

        /**
		 * 跳转到用户详情
         */
        toUserDetail:function (e) {
            var that = this;
            that.$router.push({path: '/user/information', query: {member_id: e}});
        },

        /**
		 * 跳转到订单详情
         */
        toDetail: function(e) {
            var that = this;
            that.$router.push({
                path: '/order/order_detail',
                query: {order_id: e}
            });
        },


        /**
		 * 搜索
         */
		search: function() {
			var that = this;
			that.getOrderList(that.pageNum, that.limit);
		},

		//时间转换
		formatDateTime: function(date) {
			var y = date.getFullYear();
			var m = date.getMonth() + 1;
			m = m < 10 ? ('0' + m) : m;
			var d = date.getDate();
			d = d < 10 ? ('0' + d) : d;
			var h = date.getHours();
			var minute = date.getMinutes();
			minute = minute < 10 ? ('0' + minute) : minute;
			return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
		},

        /**
		 * 取消订单
         * @param e
         */
		cancelOrder: function(e) {
			var that = this;
			that.$confirm('是否确定取消订单?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/cancel_order", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '取消成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
			})
		},

        /**
		 * 订单结算
         */
        toBalance: function(e,text) {
			var that = this;
			that.$router.push({
				path: '/order/order_detail_refund',
                query: {order_id: e,status_text:text}
			});
		},

        /**
		 * 确认退款已汇出
         */
        sureRefund:function (e) {
            let that = this;
            that.$confirm('是否确定退款已汇出?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/refund_rem", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '退款已汇出成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
		 * 处理完成
         */
        handleOver:function (e) {
            let that = this;
            that.$confirm('是否确定订单已处理完成?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/order_complete", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '处理成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
		 * 出发送货
         */
        isSetOut:function (e) {
            let that = this;
            that.$confirm('是否已出发取货？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/start_delivery", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '送货成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
		 * 用户已签收
         */
        isSignIn:function (e) {
            let that = this;
            that.$confirm('是否确定用户已签收?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/user_signin", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '签收成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },


        /**
		 * 出发取货
         */
        isGoOut:function (e) {
            let that = this;
            that.$confirm('是否已出发取货?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/start_takingoods", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '出发取货成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
		 * 用户已签还
         */
        isStill:function (e) {
            let that = this;
            that.$confirm('是否确定用户已签还？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/user_signback", {
                    token: that.token,
                    order_id: e
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '操作成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
		 * 验收
         */
        isNormal:function (e,type) {
            let that = this;
            that.$confirm('是否确定商品验收结果'+((type===1)?'正常':'异常')+'？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //请求登陆接口
                that.$http.post(that.adminApi.api_url + "/api/order/normal_accep", {
                    token: that.token,
                    order_id: e,
					type:type,
					user_id:that.user_id
                }, {
                    emulateJSON: true
                }).then(
                    function(res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '操作成功' }`
                        });
                        that.getOrderList(that.pageNum, that.limit);
                    },
                    function() {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        },

        /**
         * 跳转至详情
         */
        toGoodsDetail:function (e) {
            let that = this;
            that.$router.push({path: '/commodity/goods_detail', query: {goods_id: e}});
        }
	}
}